package com.qingke.memo.command.system;

import com.qingke.memo.command.SystemCommand;
import com.qingke.memo.pojo.CustomTag;
import com.qingke.memo.pojo.MemoCustomTag;
import com.qingke.memo.unit.MConsole;

public class DeleteCustomTag extends SystemCommand {

	@Override
	public void execute() {
		CustomTag customTag=null;
		while(true){
			String str=MConsole.promptNotEmpty("choose select custom tag by id or code (id,code):");
			if("id".equalsIgnoreCase(str))
				customTag=byId();
			else if("code".equalsIgnoreCase(str))
				customTag=byCode();
			else{
				MConsole.println("err input");
				continue;
			}
			if(customTag==null)
				return ;
			break;
		}
		
		MemoCustomTag memoCustomTag=new MemoCustomTag();
		memoCustomTag.setCustomTagId(customTag.getId());
		if(dao.isMemoCustomTagExistByCustomTag(memoCustomTag)){
			MConsole.println("please delete memo custom tag first");
			return ;
		}
		
		boolean isSuccess=dao.deleteCustomTag(customTag);
		if(isSuccess)
			MConsole.println("delete custom tag success.");
		else
			MConsole.println("delete custom tag failed.");
	}
	private CustomTag byId(){
		int id=MConsole.getInt("custom tag id:");
		if(!dao.isCustomTagExistById(id)){
			MConsole.println("the custom tag is not existed.");
			return null;
		}
		return dao.selectCustomTagById(id);
	}
	
	private CustomTag byCode(){
		String code=MConsole.promptNotEmpty("custom tag code:");
		if(!dao.isCustomTagExistByCode(code)){
			MConsole.println("the custom tag is not existed.");
			return null;
		}
		return dao.selectCustomTagByCode(code);
	}

}
